﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormRkMes
    Public usr As String
    Dim objCPCKIN As New clsCPCKIN
    Dim httpHelper As New HttpHelper
    Private Sub FormRkMes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setDgvData()
    End Sub

    Private Sub setDgvData()
        Dim dt As DataTable = objCPCKIN.getWarehouseInByStatus("0")
        dt.Columns.Add("area_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("area") = "ZAGZ01" Then
                    row("area_name") = "隔震成品库"
                End If
                If row("area") = "ZAGZ01A" Then
                    row("area_name") = "隔震成品库_A区"
                End If
                If row("area") = "ZAGZ01B" Then
                    row("area_name") = "隔震成品库_B区"
                End If
                If row("area") = "ZAGZ01C" Then
                    row("area_name") = "隔震成品库_C区"
                End If
                If row("area") = "ZAGZ01D" Then
                    row("area_name") = "隔震成品库_D区"
                End If
                If row("area") = "ZAGZ01E" Then
                    row("area_name") = "隔震成品库_E区"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        If dgv.Rows.Count > 0 Then
            For index = 0 To dgv.Rows.Count - 1
                Dim oper_num As String = dgv.Rows(index).Cells("Col_warehouse_in_count").Value.ToString()
                Dim warehousecode As String = dgv.Rows(index).Cells("Col_area").Value.ToString()
                Dim productorname As String = dgv.Rows(index).Cells("Col_prod_spec_id").Value.ToString()
                Dim MesProjectCode As String = dgv.Rows(index).Cells("Col_mes_project_code").Value.ToString()
                Dim sys_id As String = dgv.Rows(index).Cells("Col_sys_id").Value.ToString()
                mesRk(oper_num, warehousecode, productorname, MesProjectCode, sys_id)
            Next
            setDgvData()
            MsgBox("操作成功，请到同步MES入库历史记录里面，查看执行结果！", vbYes, "提示")
        Else
            MsgBox("无同步MES入库信息", vbYes, "提示")
        End If
    End Sub

    Private Sub mesRk(oper_num As String, warehousecode As String, productorname As String, MesProjectCode As String, sys_id As String)
        Dim params As String = "{"
        params &= """project_code"":""" & MesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "装配" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("入库成功") Then
            objCPCKIN.update("1", msg, usr, sys_id)
        Else
            objCPCKIN.update("01", msg, usr, sys_id)
        End If
    End Sub

End Class